package main

/*
#include "LayoutAnalysis.h"

// intentionally write the same LDFLAGS differently
#cgo CFLAGS : -I.
#cgo linux LDFLAGS: -L. -lLayout -lstdc++
#cgo darwin LDFLAGS: -L. -lLayout -lstdc++
*/
import "C"

import (
	"bytes"
	"fmt"
	"image"
	"image/jpeg"
	"image/png"
	"net/http"
	"unsafe"
)

func main() {
	fmt.Println("starting")
	resp, err := http.Get("http://7xohdn.com2.z0.glb.qiniucdn.com/word_tribe/test/img/0056f07e79a6739f7e3c58c844b81332A.png")
	fmt.Println(err)
	defer resp.Body.Close()
	img, format, err := image.Decode(resp.Body)
	fmt.Println("img", fmt.Sprintf("%T", img), err, resp.Body)
	b := img.Bounds()
	imgWidth := b.Max.X
	imgHeight := b.Max.Y
	fmt.Println("w-h:", imgHeight, imgWidth)
	var pbImage []byte
	if format == "jpeg" {
		fmt.Println("jpeg")
		buf := new(bytes.Buffer)
		err := jpeg.Encode(buf, img, nil)
		pbImage = buf.Bytes()
		fmt.Println(len(pbImage), err, imgWidth*imgHeight)
	} else if format == "png" {
		fmt.Println("png")
		buf := new(bytes.Buffer)
		err := png.Encode(buf, img)
		pbImage = buf.Bytes()
		fmt.Println(len(pbImage), err, imgWidth*imgHeight)
	} else {
		panic("unsupport image type!")
	}

	max_length := 10000
	data := new([]int)
	C.hw_SegmentHomework((unsafe.Pointer(&data)), max_length, pbImage, imgWidth, imgHeight)
}
